<?php

class OwnModule extends fvModule {

	function __construct () {
		$moduleName = strtolower(substr(__CLASS__, 0, -6));

		parent::__construct(fvSite::$fvConfig->get("modules.{$moduleName}.smarty.template"),
		fvSite::$fvConfig->get("modules.{$moduleName}.smarty.compile"),
		fvSite::$Layoult);
	}

	function showStaff() {
		$LoggedUser = fvSite::$fvSession->get("login/loggedUser");
		$request = fvRequest::getInstance();
		$m_u = $request->getRequestParameter('m_u');
		if ($m_u)
		{
			if (!$Agency = AgencyStaffManager::getInstance()->getByPk($request->getRequestParameter('id'))){
				$Agency = new AgencyStaff();
			}
			$birth_date = $request->getRequestParameter('birth');
			$m_u['birth_date'] = $birth_date['Date_Year'].'-'.$birth_date['Date_Month'].'-'.$birth_date['Date_Day'];
			$m_u['agency_id'] = $LoggedUser->agency_id;
			$Agency->updateFromRequest($m_u);
			if ($Agency->save()) {
				fvResponce::getInstance()->setHeader('Id', $Agency->getPk());
				$this->setFlash("Данные успешно сохранены", self::$FLASH_SUCCESS);
			} else {
				fvResponce::getInstance()->setHeader('X-JSON', json_encode($Agency->getValidationResult()));
				$this->setFlash("Ошибка при сохранении данных проверьте правильность введенных данных", self::$FLASH_ERROR);
			}
		}
		$AgencyStaff = AgencyStaffManager::getInstance()->getAll('agency_id='.$LoggedUser->agency_id);
		$count_staff = AgencyStaffManager::getInstance()->getCount('agency_id='.$LoggedUser->agency_id);
		$this->__assign(array(
		'AgencyStaffs' => $AgencyStaff,
		'count_staff' => $count_staff
		));
		return $this->__display("show_staff.tpl");
	}

	function showEditstaff() {
		$request = fvRequest::getInstance();
		if (!$AgencyStaff = AgencyStaffManager::getInstance()->getByPk($request->getRequestParameter('id'))){
			$AgencyStaff = new AgencyStaff();
		}
		else {
			$PersonalData = ManagerManager::getInstance()->getAll('agencystaff_id='.$AgencyStaff->getPk());
		}
		$this->__assign(array(
		'AgencyStaff' => $AgencyStaff,
		'PersonalData' => $PersonalData,
		'TestModuleManager' => TestModuleManager::getInstance(),
		));
		return $this->__display("show_editstaff.tpl");
	}
	
	function showListtest() {
		$LoggedUser = fvSite::$fvSession->get("login/loggedUser");		
		$request = fvRequest::getInstance();
		if (!$AgencyStaff = AgencyStaffManager::getInstance()->getByPk($LoggedUser->agencystaff_id)){
			$AgencyStaff = new AgencyStaff();
		}
		else {
			$PersonalData = ManagerManager::getInstance()->getAll('agencystaff_id='.$AgencyStaff->getPk());
		}
		$this->__assign(array(
		'AgencyStaff' => $AgencyStaff,
		'PersonalData' => $PersonalData,
		'TestModuleManager' => TestModuleManager::getInstance()->getAll('module_type=1'),
		'TestModule2Manager' => TestModuleManager::getInstance()->getAll('module_type=0 and  id in ('.implode(',',$AgencyStaff->countries).')'),
		));
		return $this->__display("show_listtest.tpl");
	}

	function showEdit() {
		$LoggedUser = fvSite::$fvSession->get("login/loggedUser");
		$Agency = AgencyManager::getInstance()->getByPk($LoggedUser->agency_id);
		$request = fvRequest::getInstance();
		$m_u = $request->getRequestParameter('m_u');
		if ($m_u)
		{
			$cert_date = $request->getRequestParameter('cert');
			$m_u['certificate_date'] = $cert_date['Date_Year'].'-'.$cert_date['Date_Month'].'-'.$cert_date['Date_Day'];
			$Agency->updateFromRequest($m_u);
			if ($Agency->save()) {
				fvResponce::getInstance()->setHeader('Id', $Agency->getPk());
				$this->setFlash("Данные успешно сохранены", self::$FLASH_SUCCESS);
			} else {
				fvResponce::getInstance()->setHeader('X-JSON', json_encode($Agency->getValidationResult()));
				$this->setFlash("Ошибка при сохранении данных проверьте правильность введенных данных", self::$FLASH_ERROR);
			}
			return $this->__display("show_edited.tpl");
		}
		else
		{
			$this->__assign(array(
			'Agency' => $Agency,
			'AgencyError' => $Agency->getValidationResult(),
			'CityManager' => CityManager::getInstance(),
			'LegalFormManager' => LegalFormManager::getInstance(),
			'TaxationFormManager' => TaxationFormManager::getInstance(),
			));

			return $this->__display("show_edit.tpl");
		}
	}

	function showIndex(){
		$LoggedUser = fvSite::$fvSession->get("login/loggedUser");
		if ($LoggedUser->agencystaff_id)
		{
			$AgencyStaff = AgencyStaffManager::getInstance()->getByPk($LoggedUser->agencystaff_id);
			$this->__assign(array(
			'AgencyStaff' => $AgencyStaff,
			));
			return $this->__display('show_infostaff.tpl');
		}
		else
		{
			$Agency = AgencyManager::getInstance()->getByPk($LoggedUser->agency_id);
			$this->__assign(array(
			'Agency' => $Agency,
			));
			return $this->__display('show_info.tpl');
		}

	}
}
?>